#
# @lc app=leetcode.cn id=36 lang=python3
#
# [36] 有效的数独
#

# @lc code=start
from typing import List
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        
        return self.isValidRows(board) and self.isValidColumns(board) and self.isValidCells(board)
    
    def isValidRows(self, borad:List[List[str]]) -> bool:
        numberSet = [set() for i in range(9)]
        for row in range(9):
            for col in range(9):
                if self.numberExistInSet(borad[row][col], numberSet[row]):
                    return False                   
        return True
    
    def isValidColumns(self, borad:List[List[str]]) -> bool:
        numberSet = [set() for i in range(9)]
        for col in range(9):
            for row in range(9):
                if self.numberExistInSet(borad[row][col], numberSet[col]):
                    return False                   
        return True
    
    def isValidCells(self, borad:List[List[str]]) -> bool:
        numberSet = [[set() for i in range(3)] for j in range(3)]
        for row in range(9):
            for col in range(9):
                if self.numberExistInSet(borad[row][col], numberSet[row//3][col//3]):
                    return False 
                
        return True
    # 想个更好的名字
    # 检查数值是否在set中，在的话返回true，不在返回false
    # 如果不在set中，将数字加入set。 
    # 这个函数同时做了更改和返回值，要想个更好的名字，并且这是否是个好的设计？
    def numberExistInSet(self, numberStr, set) -> bool:
        if numberStr == ".":
            return False
        else:
            number = int(numberStr)
            # 在行内数字重复了
            if number in set:
                return True
            else:
                set.add(number)
                return False
 
# print(Solution().isValidSudoku([\
# ["5","3",".",".","7",".",".",".","."],\
# ["6",".",".","1","9","5",".",".","."],\
# [".","9","8",".",".",".",".","6","."],\
# ["8",".",".",".","6",".",".",".","3"],\
# ["4",".",".","8",".","3",".",".","1"],\
# ["7",".",".",".","2",".",".",".","6"],\
# [".","6",".",".",".",".","2","8","."],\
# [".",".",".","4","1","9",".",".","5"],\
# [".",".",".",".","8",".",".","7","9"]\
# ]\
# ))
 
# @lc code=end

